$(function(){
	var _id = $('#id').val();
	if (_id != '') {
		/*** 加载数据 */
		$.post('professionalCountInfo/getDetails.shtml', {id : _id}, function(data){
			var _length = data.length;
			for (var i = 0; i < _length; i++) {
				//console.log(data[i]);
				createTableContent(data[i], $('#operate').val());
			}
		});
	} else {
		/*** 塞进一行空数据 */
		addTr();
	}
	
	/*
	$('input[name^="isImportantCk"]').each(function(){
		$(this).click(function(){
			//$(this).siblings().attr('checked',false);
			var _name = $(this).attr('name');
			$('input[name=' + _name + ']').prop('checked', false);
			$(this).prop('checked', true);
			var value = $(this).val();
			var _index = _name.substring(13);
			$('#isImportant' + _index).val(value);
		});
	});
	$('input[name^="existCommitteeCk"]').each(function(){
		$(this).click(function(){
			//$(this).siblings().attr('checked',false);
			var _name = $(this).attr('name');
			$('input[name=' + _name + ']').prop('checked', false);
			$(this).prop('checked', true);
			var value = $(this).val();
			var _index = _name.substring(16);
			$('#existCommittee' + _index).val(value);
		});
	});*/
	/** 保存 */
	$('#btnSave').click(function(){
		$("body").mask();
		$('#form').ajaxSubmit({
			url : 'professionalCountInfo/save.shtml',
			success : function(data) {
				$("body").unmask();
				if (!data.success) {
					$.messager.alert('提醒', data.message,'info');
				} else {
					$.messager.alert('提醒', data.message,'info', function() {
						$('#form').form('clear');
						parent.reSearch();
						parent.closeEasyUiWindow('win');
					});
				}
			},
			error : function (data) {
				$("body").unmask();
				$.messager.alert('提醒', '连接失败','warn');
			}
		});
	});
	
	/** 取消 */
	$('#btnCancel').click(function(){
		$('#form').form('clear');
		parent.closeEasyUiWindow('win');
	});
});

/**
 * 添加一行
 */
function addTr() {
	var rows = $('#tb tr').length;
	var index = rows - 2;
	var row_content = '<tr>'
						+ '<td><input class="input-120" type="text" name="professionCatalog" id="professionCatalog' + index +'" value="" /></td>'
						+ '<td><input class="input-120" type="text" name="professionName" id="professionName' + index +'" value="" /></td>'
						+ '<td><input class="small-input" type="text" name="professionCode" id="professionCode' + index +'" value="" /></td>'
						+ '<td>'
						+ '<label>是</label><input type="checkbox" name="isImportantCk' + index +'" value="1" />'
						+ '<label>否</label><input type="checkbox" name="isImportantCk' + index +'" value="0" />'
						+ '<input type="hidden" name="isImportant" id="isImportant' + index +'" value="" />'
						+ '</td>'
						+ '<td>'
						+ '<label>是</label><input type="checkbox" name="existCommitteeCk' + index +'"  value="1" />'
						+ '<label>否</label><input type="checkbox" name="existCommitteeCk' + index +'"  value="0" />'
						+ '<input type="hidden" name="existCommittee" id="existCommittee' + index +'"  value="" />'
						+ '</td>'
						+ '<td><input class="small-input" type="text" name="subtotal" id="subtotal' + index +'" value="" /></td>'
						+ '<td><input class="small-input" type="text" name="firstYearNum" id="firstYearNum' + index +'" value="" /></td>'
						+ '<td><input class="small-input" type="text" name="secondYearNum" id="secondYearNum' + index +'" value="" /></td>'
						+ '<td><input class="small-input" type="text" name="thirdYearNum" id="thirdYearNum' + index +'" value="" /></td>'
						+ '<td>'
						+ '<input class="small-input" type="text" name="apprenticeNum" id="apprenticeNum' + index +'" value="" />'
						+ '<br/><label>/</label><br/>'
						+ '<input class="small-input" type="text" name="orderNum" id="orderNum' + index +'" value="" />'
						+ '</td>'
						+ '<td><input class="small-input" type="text" name="graduateNum" id="graduateNum' + index +'" value="" /></td>'
						+ '<td><input class="small-input" type="text" name="doubleCertifyPercent" id="doubleCertifyPercent' + index +'" value="" /></td>'
						+ '<td><input class="small-input" type="text" name="employmentNum" id="employmentNum' + index +'" value="" /></td>'
						+ '<td><input class="small-input" type="text" name="employmentPercent" id="employmentPercent' + index +'" value="" /></td>'
						+ '<td><input class="small-input" type="text" name="employmentMatchNum" id="employmentMatchNum' + index +'" value="" /></td>'
						+ '<td><input class="small-input" type="text" name="employmentMatchPercent" id="employmentMatchPercent' + index +'" value="" /></td>'
						+ '<td><input class="small-input" type="text" name="firAvgSalary" id="firAvgSalary' + index +'" value="" /></td>'
						+ '<td><a href="javascript:void(0);" onclick="deleteTr(this);"><span class="icon-remove" title="删除" >&nbsp;</span></a></td>'
						+ '</tr>';
	$('#tb tr:eq(' + index + ')').after(row_content);
	$('input[name^="isImportantCk"]').each(function(){
		$(this).click(function(){
			//$(this).siblings().attr('checked',false);
			var _name = $(this).attr('name');
			$('input[name=' + _name + ']').prop('checked', false);
			$(this).prop('checked', true);
			var value = $(this).val();
			var _index = _name.substring(13);
			$('#isImportant' + _index).val(value);
		});
	});
	$('input[name^="existCommitteeCk"]').each(function(){
		$(this).click(function(){
			//$(this).siblings().attr('checked',false);
			var _name = $(this).attr('name');
			$('input[name=' + _name + ']').prop('checked', false);
			$(this).prop('checked', true);
			var value = $(this).val();
			var _index = _name.substring(16);
			$('#existCommittee' + _index).val(value);
		});
	});
}

/**
 * 删除某一行
 * @param obj
 */
function deleteTr(obj){
	var _a = $(obj);
	var _tr = _a.parent().parent();
	_tr.remove();
}

/**
 * 构建表格行数据
 * @param data
 */
function createTableContent(data, operate) {
	var rows = $('#tb tr').length;
	var index = rows - 2;
	var row_content = '<tr>'
						+ '<td><input class="input-120" type="text" name="professionCatalog" id="professionCatalog' + index +'" value="' + data.professionCatalog +'" /></td>'
						+ '<td><input class="input-120" type="text" name="professionName" id="professionName' + index +'" value="' + data.professionName +'" /></td>'
						+ '<td><input class="small-input" type="text" name="professionCode" id="professionCode' + index +'" value="' + data.professionCode +'" /></td>'
						+ '<td>'
						+ '<label>是</label><input type="checkbox" name="isImportantCk' + index +'" value="1" />'
						+ '<label>否</label><input type="checkbox" name="isImportantCk' + index +'" value="0" />'
						+ '<input type="hidden" name="isImportant" id="isImportant' + index +'" value="' + data.isImportant +'" />'
						+ '</td>'
						+ '<td>'
						+ '<label>是</label><input type="checkbox" name="existCommitteeCk' + index +'"  value="1" />'
						+ '<label>否</label><input type="checkbox" name="existCommitteeCk' + index +'"  value="0" />'
						+ '<input type="hidden" name="existCommittee" id="existCommittee' + index +'"  value="' + data.existCommittee +'" />'
						+ '</td>'
						+ '<td><input class="small-input" type="text" name="subtotal" id="subtotal' + index +'" value="' + data.subtotal +'" /></td>'
						+ '<td><input class="small-input" type="text" name="firstYearNum" id="firstYearNum' + index +'" value="' + data.firstYearNum +'" /></td>'
						+ '<td><input class="small-input" type="text" name="secondYearNum" id="secondYearNum' + index +'" value="' + data.secondYearNum +'" /></td>'
						+ '<td><input class="small-input" type="text" name="thirdYearNum" id="thirdYearNum' + index +'" value="' + data.thirdYearNum +'" /></td>'
						+ '<td>'
						+ '<input class="small-input" type="text" name="apprenticeNum" id="apprenticeNum' + index +'" value="' + data.apprenticeNum +'" />'
						+ '<br/><label>/</label><br/>'
						+ '<input class="small-input" type="text" name="orderNum" id="orderNum' + index +'" value="' + data.orderNum +'" />'
						+ '</td>'
						+ '<td><input class="small-input" type="text" name="graduateNum" id="graduateNum' + index +'" value="' + data.graduateNum +'" /></td>'
						+ '<td><input class="small-input" type="text" name="doubleCertifyPercent" id="doubleCertifyPercent' + index +'" value="' + data.doubleCertifyPercent +'" /></td>'
						+ '<td><input class="small-input" type="text" name="employmentNum" id="employmentNum' + index +'" value="' + data.employmentNum +'" /></td>'
						+ '<td><input class="small-input" type="text" name="employmentPercent" id="employmentPercent' + index +'" value="' + data.employmentPercent +'" /></td>'
						+ '<td><input class="small-input" type="text" name="employmentMatchNum" id="employmentMatchNum' + index +'" value="' + data.employmentMatchNum +'" /></td>'
						+ '<td><input class="small-input" type="text" name="employmentMatchPercent" id="employmentMatchPercent' + index +'" value="' + data.employmentMatchPercent +'" /></td>'
						+ '<td><input class="small-input" type="text" name="firAvgSalary" id="firAvgSalary' + index +'" value="' + data.firAvgSalary +'" /></td>';
	if ('' == operate) {
		row_content = row_content + '<td><a href="javascript:void(0);" onclick="deleteTr(this);"><span class="icon-remove" title="删除" >&nbsp;</span></a></td>';
	}
						
	row_content = row_content + '</tr>';
	$('#tb tr:eq(' + index + ')').after(row_content);
}